package com.me.study.javaCore.classLoader.customClassLoader;

import java.lang.reflect.Method;

/**
 * ClassLoader：自定义类加载器，可以加载多个相同的类
 *
 * @author ME
 * @date 2021/3/9
 */
public class TestMyClassLoader {

    public static void main(String[] args) throws Exception {
        String className = "com.me.study.javaCore.classLoader.customClassLoader.ObjA";

        MyClassLoader loader1 = new MyClassLoader();
        Class<?> objA1 = loader1.loadClass(className);
        // 创建实例对象
        Object instance = objA1.newInstance();
        Method hello = objA1.getDeclaredMethod("hello");
        hello.invoke(instance);

        System.out.println("通过 main() 调用");
        MyClassLoader loader2 = new MyClassLoader();
        Class<?> objA2 = loader2.findClass(className);
        Object instance2 = objA2.newInstance();
        Method mainMethod = objA2.getDeclaredMethod("main", String[].class);
        // 静态方法直接调用
        mainMethod.invoke(null, new Object[]{ args });

        System.out.println("\nclass 是否是多个：" + (objA1 != objA2));
    }

}
